namespace ACRSAP.Administration.Users
{
    using ASPNET.StarterKit.IssueTracker.BusinessLogicLayer;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Diagnostics;
    using System.Runtime.CompilerServices;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class UserList : Page
    {
        [AccessedThroughProperty("btnAdd")]
        private Button _btnAdd;
        [AccessedThroughProperty("UsersGrid")]
        private DataGrid _UsersGrid;
        private object designerPlaceholderDeclaration;

        public UserList()
        {
            base.Init += new EventHandler(this.Page_Init);
            base.Load += new EventHandler(this.Page_Load);
        }

        public void AddUser(object s, EventArgs e)
        {
            this.Response.Redirect("UserDetail.aspx");
        }

        private void BindUsers()
        {
            ITUserCollection userList = ITUser.GetAllUsers();
            this.SortGridData(userList, this.SortField, this.SortAscending);
            this.UsersGrid.DataSource = userList;
            this.UsersGrid.DataBind();
        }

        [DebuggerStepThrough]
        private void InitializeComponent()
        {
        }

        private void Page_Init(object sender, EventArgs e)
        {
            this.InitializeComponent();
        }

        private void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                this.BindUsers();
            }
        }

        private void SortGridData(ITUserCollection list, string sortField, bool asc)
        {
            ITUserCollection.UserFields sortCol = ITUserCollection.UserFields.InitValue;
            string _Vb_t_string_0 = sortField;
            if (StringType.StrCmp(_Vb_t_string_0, "Username", false) == 0)
            {
                sortCol = ITUserCollection.UserFields.Username;
            }
            else if (StringType.StrCmp(_Vb_t_string_0, "DisplayName", false) == 0)
            {
                sortCol = ITUserCollection.UserFields.DisplayName;
            }
            else if (StringType.StrCmp(_Vb_t_string_0, "RoleName", false) == 0)
            {
                sortCol = ITUserCollection.UserFields.RoleName;
            }
            list.Sort(sortCol, asc);
        }

        public void UsersGrid_ItemDataBound(object s, DataGridItemEventArgs e)
        {
            if ((e.Item.ItemType == ListItemType.Item) | (e.Item.ItemType == ListItemType.AlternatingItem))
            {
                ITUser objUser = (ITUser) e.Item.DataItem;
                Label lblRoleName = (Label) e.Item.FindControl("lblRoleName");
                lblRoleName.Text = objUser.RoleName;
                Label lblDisplayName = (Label) e.Item.FindControl("lblDisplayName");
                lblDisplayName.Text = objUser.DisplayName;
            }
        }

        protected void UsersGrid_Page(object sender, DataGridPageChangedEventArgs e)
        {
            this.UsersGrid.CurrentPageIndex = e.NewPageIndex;
            this.BindUsers();
        }

        private void UsersGrid_Sort(object s, DataGridSortCommandEventArgs e)
        {
            this.SortField = e.SortExpression;
            this.BindUsers();
        }

        protected virtual Button btnAdd
        {
            get
            {
                return this._btnAdd;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                if (this._btnAdd != null)
                {
                    this._btnAdd.Click -= new EventHandler(this.AddUser);
                }
                this._btnAdd = value;
                if (this._btnAdd != null)
                {
                    this._btnAdd.Click += new EventHandler(this.AddUser);
                }
            }
        }

        public bool SortAscending
        {
            get
            {
                object o = RuntimeHelpers.GetObjectValue(this.ViewState["SortAscending"]);
                return ((o == null) || BooleanType.FromObject(o));
            }
            set
            {
                this.ViewState["SortAscending"] = value;
            }
        }

        public string SortField
        {
            get
            {
                object o = RuntimeHelpers.GetObjectValue(this.ViewState["SortField"]);
                if (o == null)
                {
                    return string.Empty;
                }
                return StringType.FromObject(o);
            }
            set
            {
                if (StringType.StrCmp(value, this.SortField, false) == 0)
                {
                    this.SortAscending = !this.SortAscending;
                }
                this.ViewState["SortField"] = value;
            }
        }

        protected virtual DataGrid UsersGrid
        {
            get
            {
                return this._UsersGrid;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                if (this._UsersGrid != null)
                {
                    this._UsersGrid.ItemDataBound -= new DataGridItemEventHandler(this.UsersGrid_ItemDataBound);
                    this._UsersGrid.SortCommand -= new DataGridSortCommandEventHandler(this.UsersGrid_Sort);
                    this._UsersGrid.PageIndexChanged -= new DataGridPageChangedEventHandler(this.UsersGrid_Page);
                }
                this._UsersGrid = value;
                if (this._UsersGrid != null)
                {
                    this._UsersGrid.ItemDataBound += new DataGridItemEventHandler(this.UsersGrid_ItemDataBound);
                    this._UsersGrid.SortCommand += new DataGridSortCommandEventHandler(this.UsersGrid_Sort);
                    this._UsersGrid.PageIndexChanged += new DataGridPageChangedEventHandler(this.UsersGrid_Page);
                }
            }
        }
    }
}

